/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.demister.at.model;

import static com.demister.at.util.BitOperations.*;

/**
 *
 * @author rock
 */
public class GeneralBlockTypeSchema extends BlockTypeSchema {

    private final int REMOVE_BIT = 7;
    private final int MAJOR_BIT = 4;
    private final int MAJOR_LENGTH = 3;
    private final int MINOR_BIT = 1;
    private final int MINOR_LENGTH = 3;
    private final int DISABLE_BIT = 8;
    private final int DISABLE_LENGTH = 4;
    public final int GENERAL_MINOR = 0;

    @Override
    public boolean isRemoveType(int type) {
        return getNumberOnBits(type, REMOVE_BIT, 1) == 1;
    }

    @Override
    public boolean isRemovableSameType(int type1, int type2) {
        return isSameType(type1, type2) ||
                (isSameMajorType(type1, type2) &&
                (isRemoveType(type1) || isRemoveType(type2)));
    }

    @Override
    public boolean isCombinableSameType(int type1, int type2) {
        return isSameType(type1, type2);
    }

    @Override
    public boolean isSameMajorType(int type1, int type2) {
        return getMajorType(type1) == getMajorType(type2);
    }

    @Override
    public boolean isSameType(int type1, int type2) {
        return isSameMajorType(type1, type2) &&
                getMinorType(type1) == getMinorType(type2);
    }

    @Override
    public boolean isDisable(int type) {
        return getDisableCount(type) != 0;
    }

    @Override
    public int getDisableCount(int type) {
        return getNumberOnBits(type, DISABLE_BIT, DISABLE_LENGTH);
    }

    @Override
    public int setDisableCount(int type, int count) {
        if (count >= (1 << DISABLE_LENGTH)) {
            count = (1 << DISABLE_LENGTH) - 1;
        } else if (count < 0) {
            count = 0;
        }

        return setNumberOnBits(type, DISABLE_BIT, DISABLE_LENGTH, count);
    }

    @Override
    public int getMajorType(int type) {
        return getNumberOnBits(type, MAJOR_BIT, MAJOR_LENGTH);
    }

    @Override
    public int getMinorType(int type) {
        return getNumberOnBits(type, MINOR_BIT, MINOR_LENGTH);
    }

}
